import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_day_01/DetailPage.dart';
import 'package:flutter_day_01/model/User.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _counter = 0;

  // final List<User> list = null;

  List<User> get list {
    List<User> tmps = List();
    for (int i = 0; i < 10; i++) {
      User user01 = User(name = '盖文' + '$i', 18 + i,
          'https://upload-images.jianshu.io/upload_images/106726-fe5ac7ee7f440d51.jpg');
      tmps.add(user01);
    }
    return tmps;
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void _pushSaved(User user) {
    Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return DetailPage(user: user);
    }));
  }

  @override
  Widget build(Object context) {
    return Scaffold(
        // appBar: AppBar(
        //   title: Text(widget.title),
        //   backgroundColor: Colors.blue,
        // ),
        body: ListView.separated(
            padding: const EdgeInsets.all(16.0),
            separatorBuilder: (BuildContext context, int index) =>
                const Divider(),
            itemCount: list.length,
            itemBuilder: (context, i) => _listViewCell(context, i)));
  }

// cell
  StatelessWidget _listViewCell(BuildContext context, int row) {
    User user = list[row];

    return GestureDetector(
      child: Container(
        height: 60,
        child: Row(
          children: [
            Container(
                width: 45,
                height: 45,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(22.5),
                  image: DecorationImage(
                      image: NetworkImage(user.avatarUrl), fit: BoxFit.cover),
                )),
            Container(
                padding: const EdgeInsets.fromLTRB(8, 0, 0, 0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(user.name, textAlign: TextAlign.left),
                    Text(
                      '${user.age}',
                      textAlign: TextAlign.left,
                      maxLines: 2,
                    )
                  ],
                ))
          ],
        ),
      ),
      onTap: () {
        _pushSaved(user);
      },
    );
  }
}
